package cn.math.appmath.dao.impl;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import cn.math.appmath.bean.ActivityBean;
import cn.math.appmath.bean.PageBean;
import cn.math.appmath.dao.ActivityDao;
import cn.math.appmath.dao.SomeConstants;
import cn.math.appmath.entity.Activity;

/**
 * A data access object (DAO) providing persistence and search support for
 * Activity entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see cn.math.appmath.entity.Activity
 * @author MyEclipse Persistence Tools
 */

public class ActivityDaoImpl extends HibernateDaoSupport implements ActivityDao {
	private static final Log log = LogFactory.getLog(ActivityDaoImpl.class);

	public boolean save(Activity transientInstance) {
		log.debug("saving Activity instance");
		try {
			Session session = getSession();
			Transaction tx = session.beginTransaction();
			session.saveOrUpdate(transientInstance);
			tx.commit();
			session.close();
			log.debug("save successful");
			return true;
		} catch (RuntimeException re) {
			log.error("save failed", re);
			re.printStackTrace();
			return false;
		}
	}

	public boolean delete(int id) {
		log.debug("deleting Activity instance");
		try {
			String hql = "delete from Activity where ano=?";
			Query queryObject = getSession().createQuery(hql);
			queryObject.setParameter(0, id);
			queryObject.executeUpdate();
			log.debug("delete successful");
			return true;	
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			re.printStackTrace();
			return false;
		}
	}
	
	public boolean batchDelete(Integer[] ids) {
		log.debug("batchDelete Activity instance");
		try{
			String hql = "delete from Activity where ano in (:ids)";
			Query query = getSession().createQuery(hql);
			query.setParameterList("ids", ids);
			query.executeUpdate();
			log.debug("batchDelete successful");
			return true;
		}catch (Exception ex){
			log.error("batchDelete failed", ex);
			ex.printStackTrace();
			return false;
		}
	}
	
	public Activity findById(java.lang.Integer id) {
		log.debug("getting Activity instance with id: " + id);
		try {
			Activity instance = (Activity) getSession().get(
					"cn.math.entity.Activity", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<Activity> findByExample(Activity instance) {
		log.debug("finding Activity instance by example");
		try {
			List<Activity> results = (List<Activity>)getSession().createCriteria(
					"cn.math.entity.Activity").add(Example.create(instance))
					.list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			re.printStackTrace();
			return null;
		}
	}

	public List<Activity> findByProperty(String propertyName, Object value) {
		log.debug("finding Activity instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Activity as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return (List<Activity>)queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public ActivityBean findAll(int page) {
		log.debug("finding all Activity instances");
		try {
			String queryString = "select count(*) from Activity";
			Query queryObject = getSession().createQuery(queryString);
			int allRow = Integer.parseInt(String.valueOf(queryObject.uniqueResult()));
			int totalPage = PageBean.countTotalPage(SomeConstants.PageSize, allRow);
			int currentPgae;
			if(page == totalPage)
				currentPgae = totalPage;
			else
				currentPgae = PageBean.countCurrentPage(page);
			int currentRow = PageBean.countCurrentRow(SomeConstants.PageSize, currentPgae);
			queryString = "from Activity order by atime desc";
			queryObject = getSession().createQuery(queryString);
			queryObject.setFirstResult(currentRow);
			queryObject.setMaxResults(SomeConstants.PageSize);
			List<Activity> list;
			if(allRow != 0){
				list = (List<Activity>)queryObject.list();
			}else{
				list = null;
			}
			return new ActivityBean(totalPage, currentPgae, list, allRow);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
}